#include "headers.h"

ControlTable::ControlTable(QList<int> key1vals, QList<QString> key2vals)
{
    for(int i=0; i<key1vals.length(); ++i)
        TranslatorK1.insert(key1vals[i], i);
    for(int i=0; i<key2vals.length(); ++i)
        TranslatorK2.insert(key2vals[i], i);
    values=new Cell*[TranslatorK1.count()];
    for(int i=0; i<TranslatorK1.count(); ++i)
        values[i]=new Cell[TranslatorK2.count()];
}

ControlTable::~ControlTable()
{
    for(int i=0; i<TranslatorK1.count(); ++i)
        delete values[i];
    delete values;
}

int ControlTable::SetValue(int key1, QString key2, Cell cell)
{
    if(!TranslatorK1.contains(key1))
        return -1;
    if(!TranslatorK2.contains(key2))
        return -2;
    values[TranslatorK1[key1]][TranslatorK2[key2]]=cell;
    return 0;
}

int ControlTable::GetValue(int key1, QString key2, Cell* bufer) const
{
    if(!TranslatorK1.contains(key1))
        return -1;
    if(!TranslatorK2.contains(key2))
        return -2;
    *bufer=values[TranslatorK1[key1]][TranslatorK2[key2]];
    return 0;
}

QList<int> ControlTable::keys1() const
{
    return TranslatorK1.keys();
}

QList<QString> ControlTable::keys2() const
{
    return TranslatorK2.keys();
}

QStringList ControlTable::keys1string() const
{
    QStringList result;
    foreach(int key, TranslatorK1.keys())
        result.append(QString::number(key));
    return result;
}

int ControlTable::GetWidth() const
{
    return TranslatorK2.count();
}

int ControlTable::GetHeight() const
{
    return TranslatorK1.count();
}
